import { ref, computed, watch, nextTick, getCurrentInstance, onMounted } from 'vue';
import type { Ref } from 'vue';
import type { hooksParam, CommonState } from '.';
import { Enum } from '../env';
type RefresherCtx = {
	finalRefresherEnabled: Ref<boolean | undefined>;
	handleGoF2: Function;
	_doRefresherLoad: Function;
	_emitTouchmove: Function;
	checkRefresherStatus(beyond: boolean, value: number): void;
};
const useNVue = (
	ctx: ReturnType<typeof getCurrentInstance>,
	option: Ref<hooksParam>,
	emit: ReturnType<typeof defineEmits>,
	commonState: CommonState,
	handleRefresherEnd: Function,
	refresherCtx: RefresherCtx,
	oldScrollTop: Ref<number>,
	doChatRecordLoadMore: Function
) => {
	// #ifdef APP-NVUE
	const nLoadingMoreFixedHeight = ref(false);
	watch(
		() => option.value.useChatRecordMode,
		(v) => {
			if (v) {
				nLoadingMoreFixedHeight.value = false;
			}
		}
	);

	const finalNvueListIs = computed(() => {
		if (option.value.usePageScroll) return 'view';
		const nvueListIsLowerCase = option.value.nvueListIs.toLowerCase();
		if (['list', 'waterfall', 'scroller'].indexOf(nvueListIsLowerCase) !== -1) return nvueListIsLowerCase;
		return 'list';
	});
	const nWaterfallColumnCount = computed(() => {
		if (finalNvueListIs.value !== 'waterfall') return 0;
		return _nGetWaterfallConfig('column-count', 2);
	});
	const nWaterfallColumnWidth = computed(() => {
		return _nGetWaterfallConfig('column-width', 'auto');
	});
	const nWaterfallColumnGap = computed(() => {
		return _nGetWaterfallConfig('column-gap', 'normal');
	});
	const nWaterfallLeftGap = computed(() => {
		return _nGetWaterfallConfig('left-gap', 0);
	});
	const nWaterfallRightGap = computed(() => {
		return _nGetWaterfallConfig('right-gap', 0);
	});
	const nViewIs = computed(() => {
		const is = finalNvueListIs.value;
		return is === 'scroller' || is === 'view' ? 'view' : is === 'waterfall' ? 'header' : 'cell';
	});

	const finalNvueSuperListIs = computed(() => {
		return option.value.usePageScroll ? 'view' : 'scroller';
	});
	const finalNvueRefresherEnabled = computed(() => {
		return finalNvueListIs.value !== 'view' && refresherCtx.finalRefresherEnabled.value && !option.value.useChatRecordMode;
	});

	let nListIsDragging = false;
	// 列表滚动时触发
	const handleNVueScroll = (e: { detail: { scrollTop: number; scrollHeight: number }; contentOffset: { y: number }; isDragging: boolean }) => {
		const contentOffsetY = -e.contentOffset.y;
		oldScrollTop.value = contentOffsetY;
		nListIsDragging = e.isDragging;
	};
	const nListRef = ref();
	const nRefresherLoading = ref(false);
	// 下拉刷新结束
	const _nRefresherEnd = (doEnd = true) => {
		if (doEnd) {
			_nDoRefresherEndAnimation();
			!option.value.usePageScroll && nListRef.value?.resetLoadmore?.();
			nRefresherLoading.value = false;
		}
	};
	
	// 下拉刷新刷新中
	const _nOnRrefresh = () => {
		// 进入刷新状态
		nRefresherLoading.value = true;
		if (commonState.refresherStatus === Enum.Refresher.GoF2) {
			refresherCtx.handleGoF2();
			nextTick(() => {
				_nRefresherEnd();
			});
		} else {
			commonState.refresherStatus = Enum.Refresher.Loading;
			refresherCtx._doRefresherLoad();
		}
	};
	// 下拉刷新下拉中
	const _nOnPullingdown = (e: any) => {
		if (commonState.refresherStatus === Enum.Refresher.Loading || (commonState.isIos && !nListIsDragging)) return;
		refresherCtx._emitTouchmove(e);
		let { viewHeight, pullingDistance } = e;
		// 更新下拉刷新状态
		refresherCtx.checkRefresherStatus(pullingDistance >= viewHeight, pullingDistance - viewHeight);
	};

	// 执行主动触发下拉刷新动画
	const _nDoRefresherEndAnimation = () => {
		handleRefresherEnd();
	};
	// 滚动到底部
	const handleNVueLoadmore = () => {
		option.value.useChatRecordMode ? doChatRecordLoadMore() : emit('scrolltolower', 'toBottom');
	};
	// 获取nvue waterfall单项配置
	const _nGetWaterfallConfig = (key: string, defaultValue: any) => {
		return option.value.nvueWaterfallConfig?.[key] || defaultValue;
	};
	// const nRefresherWidth = ref(uni.upx2px(750))
	// // 更新nvue 下拉刷新view容器的宽度
	// const _nUpdateRefresherWidth = () => {
	// 	sleep().then(() => {
	// 		nextTick(()=>{
	// 			getNvueRect(nListRef.value).then(size => {
	// 				if (size) {
	// 					nRefresherWidth.value = size?.width || nRefresherWidth.value;
	// 				}
	// 			})
	// 		})
	// 	})
	// }

	onMounted(() => {
		//旋转屏幕时更新宽度
		// uni.onWindowResize((res) => {
		// 	_nUpdateRefresherWidth();
		// })
	});

	return {
		nLoadingMoreFixedHeight,
		finalNvueListIs,
		nWaterfallColumnCount,
		nWaterfallColumnWidth,
		nWaterfallColumnGap,
		nWaterfallLeftGap,
		nWaterfallRightGap,
		nViewIs,
		finalNvueSuperListIs,
		finalNvueRefresherEnabled,
		handleNVueScroll,
		nListRef,
		_nRefresherEnd,
		nRefresherLoading,
		_nOnRrefresh,
		_nOnPullingdown,
		_nDoRefresherEndAnimation,
		handleNVueLoadmore,
		_nGetWaterfallConfig
	};
	// #endif
};

export default useNVue;
